﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.TemplateLanguage.Design;
using MethodWorx.CMS.TemplateLanguage.Properties;

namespace MethodWorx.CMS.TemplateLanguage.Parsers.Page
{
    public class Breadcrumb : IDesignable
    {
        public void Design(IDesigner designer)
        {
            designer.AddControl("Page:Breadcrumb")
                .AddContentBeforeChildren("<span>Breadcrumb</span>")
                .AddChildControl("default")
                .AddChildControl("seperator")
                .AddChildControl("current");
        }

        public object Parse(Web.CmsRequestContext context, IEnumerable<object> stack, Dictionary<string, object> parameters)
        {
            var page = stack.OfType<MethodWorx.CMS.Content.Page>().FirstOrDefault();
            if (page != null)
            {
    
                //  ok, we have the page, 
                List<EnumerableResult> results = new List<EnumerableResult>();

                var current = true;
                while(page != null)
                {
                    if(!current)
                    {
                        results.Add(new EnumerableResult()
                                        {
                                            Names = new string[]
                                                        {
                                                            "seperator"
                                                        },
                                            Result = page
                                        });
                    }
                    results.Add(new EnumerableResult()
                                    {
                                        Names = new string[]
                                                    {
                                                        current ? "current" : "default", "default"
                                                    },
                                        Result = page
                                    });
                    current = false;
                    page = page.Parent;
                }
                results.Reverse();
                return results;
            }
            else
                return null;
        }

        public string Name
        {
            get { return "Breadcrumb"; }
        }

        public string Namespace
        {
            get { return "Page"; }
        }
    }
}
